﻿/*
* 创建者：LIYINFAI
* 创建时间：2023/12/24 9:35:26
* 描述：
* --------------------------------------
* 修改说明：
* --------------------------------------
*/
using System;
using System.Windows.Forms;
using CommonLibrary;
using CommonLibrary.Forms;
namespace BusinessLibrary
{
    /// <summary>
    /// 选项界面
    /// </summary>
    public partial class FormMESOptions : HWFrmWithTitle
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public FormMESOptions()
        {
            InitializeComponent();
        }
        private void tabControlSettings_SelectedIndexChanged(object sender, EventArgs e)
        {
        }
        /// <summary>
        /// 窗体加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FormOptions_Load(object sender, EventArgs e)
        {
            LoadMESSettings();
            InitApiSetting();
        }
        private void InitApiSetting()
        {
            CommonHelper.CreateTextGridColumn(this.dataGridView, "Name", "名称", DataGridViewContentAlignment.MiddleCenter, 220, false, true);
            CommonHelper.CreateTextGridColumn(this.dataGridView, "Url", "地址", DataGridViewContentAlignment.MiddleCenter, 600, true);
            CommonHelper.CreateTextGridColumn(this.dataGridView, "Timeout", "超时时间（毫秒）", DataGridViewContentAlignment.MiddleCenter, 150);
            CommonHelper.CreateTextGridColumn(this.dataGridView, "RetryTimes", "失败重试次数", DataGridViewContentAlignment.MiddleCenter, 140);
            this.dataGridView.DoubleBufferedDataGirdView(true);
            this.dataGridView.InitStyle();

            this.dataGridView.Rows.Add(6);

            var mesBusiness = MesBusiness.Instance();
            var loginRow = this.dataGridView.Rows[0];
            loginRow.Cells[0].Value = "登录接口";
            loginRow.Cells[1].Value = mesBusiness.LoginApi;
            loginRow.Cells[2].Value = mesBusiness.LoginApiTimeout;
            loginRow.Cells[3].Value = mesBusiness.LoginApiRetryTimes;


            var inStationRow = this.dataGridView.Rows[1];
            inStationRow.Cells[0].Value = "进站接口";
            inStationRow.Cells[1].Value = mesBusiness.InStationApi;
            inStationRow.Cells[2].Value = mesBusiness.InStationTimeout;
            inStationRow.Cells[3].Value = mesBusiness.InStationRetryTimes;


            var outStationRow = this.dataGridView.Rows[2];
            outStationRow.Cells[0].Value = "出站接口";
            outStationRow.Cells[1].Value = mesBusiness.OutStationApi;
            outStationRow.Cells[2].Value = mesBusiness.OutStationTimeout;
            outStationRow.Cells[3].Value = mesBusiness.OutStationRetryTimes;


            var statusRow = this.dataGridView.Rows[3];
            statusRow.Cells[0].Value = "设备状态接口";
            statusRow.Cells[1].Value = mesBusiness.DeviceStatusApi;
            statusRow.Cells[2].Value = mesBusiness.DeviceStatusTimeout;
            statusRow.Cells[3].Value = mesBusiness.DeviceStatusRetryTimes;

            var alarmRow = this.dataGridView.Rows[4];
            alarmRow.Cells[0].Value = "报警接口";
            alarmRow.Cells[1].Value = mesBusiness.AlarmApi;
            alarmRow.Cells[2].Value = mesBusiness.AlarmTimeout;
            alarmRow.Cells[3].Value = mesBusiness.AlarmRetryTimes;

            var resultRow = this.dataGridView.Rows[5];
            resultRow.Cells[0].Value = "设备结果接口";
            resultRow.Cells[1].Value = mesBusiness.DeviceResultApi;
            resultRow.Cells[2].Value = mesBusiness.DeviceResultTimeout;
            resultRow.Cells[3].Value = mesBusiness.DeviceResultRetryTimes;
        }
        /// <summary>
        /// 加载设置内容
        /// </summary>
        private void LoadMESSettings()
        {
            MesBusiness mesBusiness = MesBusiness.Instance();
            this.textBoxMESUrl.Text = mesBusiness.Url;
            this.textBoxMaterialCode.Text = mesBusiness.MaterialCode;
            this.textBoxWorkOrderNum.Text = mesBusiness.WorkOrderNum;
        }
        /// <summary>
        /// 保存按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonSave_Click(object sender, EventArgs e)
        {
            if (GlobalStatus.APP == EAppStatus.RUN)
            {
                HWMessageBox.Show(this, "设备运行中，不允许修改参数");
                return;
            }
            bool checkPermission = UserHelper.CheckPermission(this, 3);
            if (checkPermission)
            {
                bool isValid = true;
                var rows = this.dataGridView.Rows;
                var url = this.textBoxMESUrl.Text.Trim();
                var materialCode = this.textBoxMaterialCode.Text.Trim();
                var workOrderNum = this.textBoxWorkOrderNum.Text.Trim();

                if (string.IsNullOrWhiteSpace(url))
                {
                    isValid = false;
                    HWMessageBox.Show(this, "MES地址有误");
                }
                else if (string.IsNullOrWhiteSpace(materialCode))
                {
                    isValid = false;
                    HWMessageBox.Show(this, "物料编码有误");
                }
                else if (string.IsNullOrWhiteSpace(workOrderNum))
                {
                    isValid = false;
                    HWMessageBox.Show(this, "工单编码有误");
                } 
                if (rows == null || rows.Count != 6)
                {
                    isValid = false;
                }

                if (isValid)
                {
                    MesBusiness mesBusiness = MesBusiness.Instance();
                    mesBusiness.Url = url;
                    #region 接口相关
                    int timeout = 2000, retryTimes = 0;
                    var apiString = CommonHelper.GetCellStringValue(rows[0], "Url");
                    var timeoutString = CommonHelper.GetCellStringValue(rows[0], "Timeout");
                    var retryTimesString = CommonHelper.GetCellStringValue(rows[0], "RetryTimes");
                    if (int.TryParse(timeoutString, out timeout) && int.TryParse(retryTimesString, out retryTimes) && !string.IsNullOrWhiteSpace(apiString))
                    {
                        mesBusiness.LoginApi = apiString;
                        mesBusiness.LoginApiTimeout = timeout;
                        mesBusiness.LoginApiRetryTimes = retryTimes;
                    }

                    apiString = CommonHelper.GetCellStringValue(rows[1], "Url");
                    timeoutString = CommonHelper.GetCellStringValue(rows[1], "Timeout");
                    retryTimesString = CommonHelper.GetCellStringValue(rows[1], "RetryTimes");
                    if (int.TryParse(timeoutString, out timeout) && int.TryParse(retryTimesString, out retryTimes) && !string.IsNullOrWhiteSpace(apiString))
                    {
                        mesBusiness.InStationApi = apiString;
                        mesBusiness.InStationTimeout = timeout;
                        mesBusiness.InStationRetryTimes = retryTimes;
                    }

                    apiString = CommonHelper.GetCellStringValue(rows[2], "Url");
                    timeoutString = CommonHelper.GetCellStringValue(rows[2], "Timeout");
                    retryTimesString = CommonHelper.GetCellStringValue(rows[2], "RetryTimes");
                    if (int.TryParse(timeoutString, out timeout) && int.TryParse(retryTimesString, out retryTimes) && !string.IsNullOrWhiteSpace(apiString))
                    {
                        mesBusiness.OutStationApi = apiString;
                        mesBusiness.OutStationTimeout = timeout;
                        mesBusiness.OutStationRetryTimes = retryTimes;
                    }


                    apiString = CommonHelper.GetCellStringValue(rows[3], "Url");
                    timeoutString = CommonHelper.GetCellStringValue(rows[3], "Timeout");
                    retryTimesString = CommonHelper.GetCellStringValue(rows[3], "RetryTimes");
                    if (int.TryParse(timeoutString, out timeout) && int.TryParse(retryTimesString, out retryTimes) && !string.IsNullOrWhiteSpace(apiString))
                    {
                        mesBusiness.DeviceStatusApi = apiString;
                        mesBusiness.DeviceStatusTimeout = timeout;
                        mesBusiness.DeviceStatusRetryTimes = retryTimes;
                    }


                    apiString = CommonHelper.GetCellStringValue(rows[4], "Url");
                    timeoutString = CommonHelper.GetCellStringValue(rows[4], "Timeout");
                    retryTimesString = CommonHelper.GetCellStringValue(rows[4], "RetryTimes");
                    if (int.TryParse(timeoutString, out timeout) && int.TryParse(retryTimesString, out retryTimes) && !string.IsNullOrWhiteSpace(apiString))
                    {
                        mesBusiness.AlarmApi = apiString;
                        mesBusiness.AlarmTimeout = timeout;
                        mesBusiness.AlarmRetryTimes = retryTimes;
                    }

                    apiString = CommonHelper.GetCellStringValue(rows[5], "Url");
                    timeoutString = CommonHelper.GetCellStringValue(rows[5], "Timeout");
                    retryTimesString = CommonHelper.GetCellStringValue(rows[5], "RetryTimes");
                    if (int.TryParse(timeoutString, out timeout) && int.TryParse(retryTimesString, out retryTimes) && !string.IsNullOrWhiteSpace(apiString))
                    {
                        mesBusiness.DeviceResultApi = apiString;
                        mesBusiness.DeviceResultTimeout = timeout;
                        mesBusiness.DeviceResultRetryTimes = retryTimes;
                    }
                    #endregion

                    mesBusiness.Save();
                    this.DialogResult = DialogResult.OK;
                    HWMessageBox.Show(this, "保存成功");
                }
            }
        }
        private void FormOptions_Shown(object sender, EventArgs e)
        {
        }
        private void FormOptions_FormClosing(object sender, FormClosingEventArgs e)
        {
        }
    }
}
